/*
 * @lc app=leetcode.cn id=495 lang=typescript
 *
 * [495] 提莫攻击
 */

// @lc code=start
function findPoisonedDuration1(timeSeries: number[], duration: number): number {
  if (timeSeries.length == 0) {
    return 0;
  }
  let res = 0;
  for (let i = 1; i < timeSeries.length; i++) {
    let gap = timeSeries[i] - timeSeries[i - 1];
    if (duration > gap) {
      res += gap;
    } else {
      res += duration;
    }
  }
  return res + duration;
}
// @lc code=end

// @lc code=start
function findPoisonedDuration2(timeSeries: number[], duration: number): number {
  let n = timeSeries.length;
  if (n == 0) return 0;
  let total = 0;
  for (let i = 0; i < n - 1; ++i) {
    total += Math.min(timeSeries[i + 1] - timeSeries[i], duration);
  }
  return total + duration;
}
// @lc code=end

// @lc code=start
function findPoisonedDuration3(timeSeries: number[], duration: number): number {
  let anx = 0;
  if (timeSeries.length == 0) {
    return anx;
  }
  for (let i = 1; i < timeSeries.length; i++) {
    anx +=
      timeSeries[i - 1] + duration - timeSeries[i] < 0
        ? duration
        : timeSeries[i] - timeSeries[i - 1];
  }
  anx += duration;
  return anx;
}
// @lc code=end
